package com.androidproject.activity;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.androidproject.R;
import com.androidproject.modele.Article;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class DetailArticleActivity extends Activity {

	private Intent intentAccueil;
	private Intent intentClassement;
	private Intent intentCalendrier;
	private Intent intentProgrammeTV;
	private Intent intentFacebook;
	private Intent intentTwitter;
	private Intent intent;
	private Article article;
	private TextView titre;
	private TextView content;
	private ImageView image;
	
	private Button btnPartage;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail_article);
		
		titre = (TextView)findViewById(R.id.tvTitre);
		content = (TextView)findViewById(R.id.tvContenue);
		image = (ImageView)findViewById(R.id.articleImage);
		
		intent = getIntent();
		
		article = (Article) intent.getExtras().get("article");
		
		titre.setText(article.getTitle());
		content.setText(article.getContenu());
		Bitmap bitmap = null;
		try {
			URL urlImage = new URL(article.getImage());
			HttpURLConnection connection = (HttpURLConnection) urlImage.openConnection();
			InputStream inputStream = connection.getInputStream();
			bitmap = BitmapFactory.decodeStream(inputStream);
			image.setImageBitmap(bitmap);
			} catch (MalformedURLException e) {
			e.printStackTrace();
			} catch (IOException e) {
			e.printStackTrace();
			}
		
		btnPartage = (Button)findViewById(R.id.btnPartage);
		btnPartage.setOnClickListener(
		        new OnClickListener() {
		    		@Override
		    		public void onClick(View v) {
		    			final Intent MessIntent = new Intent(Intent.ACTION_SEND);
		    	        	MessIntent.setType("text/plain");
		    	        	MessIntent.putExtra(Intent.EXTRA_TEXT, titre.getText().toString());
		    	        	DetailArticleActivity.this.startActivity(Intent.createChooser(MessIntent, "Partager ..."));
		    		}
		        }
		    );
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		menu.add(0, 1, 0, "News").setIcon(R.drawable.logo_journal);
		menu.add(0, 2, 0, "Classement").setIcon(R.drawable.logo_classement);
		menu.add(0, 3, 0, "Programme TV").setIcon(R.drawable.logo_tv);
		menu.add(0, 4, 0, "Calendrier").setIcon(R.drawable.logo_calendrier);
		menu.add(0, 5, 0, "Facebook").setIcon(R.drawable.logo_facebook);
		menu.add(0, 6, 0, "Twitter").setIcon(R.drawable.logo_twitter);
	    
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch (item.getItemId()) {
		case 1:
			intentAccueil = new Intent(this, MainActivity.class);
			startActivity(intentAccueil);
			return true;
			
		case 2:
			intentClassement = new Intent(this, ClassementActivity.class);
			startActivity(intentClassement);
			return true;
			
		case 3:
			intentProgrammeTV = new Intent(this, ProgrammeTvActivity.class);
			startActivity(intentProgrammeTV);
			return true;
			
		case 4:
			intentCalendrier = new Intent(this, CalendrierActivity.class);
			startActivity(intentCalendrier);
			return true;
		
		case 5:
			intentFacebook = new Intent(this, FacebookActivity.class);
			startActivity(intentFacebook);
			return true;
			
		case 6:
			intentTwitter = new Intent(this, TwitterActivity.class);
			startActivity(intentTwitter);
			return true;
			
		default:
			return true;
		}
	}
}
